package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import service.SellerService;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

import entities.SellerEntity;

public class SellerCountServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public SellerCountServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		
		String date = request.getParameter("date");
		//1,获得一个商家list
		//2,遍历商家，分别计算已支付未支付和总额，再加上商家id作为一个json对象放入json数组中
		//3,把json数组输出到客户端
		List<SellerEntity> sellerList = new ArrayList();
		try {
			sellerList = SellerService.getSellers(1);
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		//Map<String,Double> sellerMap1 = new HashMap();
		Map<String,Double> sellerMap2 = new HashMap();
		try {
		//	sellerMap1 = SellerService.getSellerCount(date);
			sellerMap2 = SellerService.getSellerCounted(date);
			//System.out.println("@#@#@#@#@#@#@#@#"+result+"@#@#@#@#@#@#@#");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JsonArray array = new JsonArray();
		for(SellerEntity seller:sellerList){
			JsonObject obj = new JsonObject();
			Double counted = sellerMap2.get(seller.getName());
			counted = counted == null ? 0:counted;
			//Double uncounted = sellerMap1.get(seller.getName());
			//uncounted = uncounted == null ? 0:uncounted;
			//Double total = counted + uncounted;
			obj.addProperty("id", seller.getId());
			obj.addProperty("counted", counted);
			//obj.addProperty("uncounted", uncounted);
			//obj.addProperty("total", total);
			array.add(obj);
		}
		
		//out.print(result);
		out.println(array.toString());
		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
